package com.exam.system.util;

import com.github.pagehelper.PageInfo;

import java.util.Map;
import java.util.StringJoiner;

/**
 * @Author LY
 * @create 2023/10/7 10:57
 * @Description 通用返回
 */
public class BodyUtil {

    private int status = 200;
    private String message = "";

    private Object data;

    public static BodyUtil build() {
        return new BodyUtil();
    }

    public int getStatus() {
        return status;
    }

    public String getMessage() {
        return message;
    }

    public Object getData() {
        return data;
    }

    public BodyUtil ok(String message) {
        this.status = 200;
        this.message = message;
        return this;
    }

    public BodyUtil ok(String message, Object data) {
        this.status = 200;
        this.message = message;
        this.data = data;
        return this;
    }

    public BodyUtil ok(String msg, Map<String, Object> data) {
        this.status = 200;
        this.message = msg;
        this.data = data;
        return this;
    }

    public BodyUtil ok(String msg, PageInfo pageInfo) {
        this.status = 200;
        this.message = msg;
        this.data = pageInfo.getList();
        return this;
    }

    public BodyUtil fail(String msg) {
        this.status = 400;
        this.message = msg;
        return this;
    }

    public BodyUtil fail(String msg, Map<String, Object> data) {
        this.status = 400;
        this.message = msg;
        this.data = data;
        return this;
    }

    public BodyUtil internalServerError(String msg) {
        this.status = 500;
        this.message = msg;
        return this;
    }

    public BodyUtil internalServerError(String msg, Map<String, Object> data) {
        this.status = 500;
        this.message = msg;
        this.data = data;
        return this;
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", BodyUtil.class.getSimpleName() + "[", "]")
                .add("status=" + status)
                .add("message='" + message + "'")
                .add("data=" + data)
                .toString();
    }
}
